const Redis = require('../util/redis');
const jwt = require('jsonwebtoken');
const {jwt: {SECRET}} = require('../../config/config.json');  // 加密盐值
const verirytoken = require('../util/verifytoken');
module.exports = class UserAccount {
    constructor () {}
    /**
     * 更新token
     */
    static async exchangetoken (ctx) {
        const {state} = ctx;
        if (!state || !state.user) {
            return ctx.body = {
                code: '00002',
                msg: '未找到用户',
                data: null
            }
        } else {
            const user = state.user;
            const token = jwt(user, SECRET, {expiresIn: '30m'})
            ctx.body = {
                code: '00001',
                data: token,
                msg: '更新成功'
            }
        }
    }
    /**
     * 
     * @param {Object} ctx 上下文
     */
    static async login (ctx) {
        const { request: { body }, db } = ctx;
        if (!body.username || !body.password) {
            return ctx.body = {
                code: '00002',
                msg: '参数错误',
                data: null
            }
        }
        if (!body.username || !body.password) {
            return ctx.body = {
                code: '00004',
                msg: '参数错误',
                data: null
            }
        }
        let result = await db.useraccount.findOne({where: body});
        if (result) {
            // 过期时间10秒
            const token = jwt.sign({
                name: result.username,
                id: result.id
            }, SECRET, {expiresIn: '30m'});
            return ctx.body = {
                code: '00001',
                data: token,
                msg: '登陆成功'
            }
        } else {
            ctx.body = {
                code: '00002',
                data: null,
                msg: '用户名或者密码错误'
            }
        }
    }
    /**
     * 注册
     */
    static async create (ctx) {
        const {request: { body }, db} = ctx;
        if (!body.username || !body.password) {
            return ctx.body = {
                code: '00002',
                data: null,
                msg: '参数错误'
            }
        }
        // 查询并创建， result 是一个Array, result[0] 查询的或者新增的数据，  result[1]: 如果是新增数据 true  如果是已有的数据  false
        let result = await db.useraccount.findOrCreate({where: {username: body.username}, defaults: {password: body.password}});
        if (!result) {
            return ctx.body = {
                code: '00002',
                data: null,
                msg: '请重试'
            }
        }
        if (result[1]) {
            const token = jwt.sign({
                name: result[0].username,
                id: result[0].id
            }, SECRET, {expiresIn: '30m'});
            ctx.body = {
                code: '00001',
                data: token,
                msg: '注册成功'
            }
        } else {
            ctx.body = {
                code: '00002',
                data: null,
                msg: '账号已存在'
            }
        }
    }
    /**
     * 获取列表
     * @param {Object} ctx app 上下文  ctx对象
     * @param {Object} ctx.db  数据库操作 ctx.db.useraccount 对应model下 useraccount数据模型。
     */
    static async list (ctx) {
        const {request: {query}, db} = ctx;
        /**
         * limit   限制取多少条内容
         * offset  偏移： 从第几个位置开始取数据
         * where   限制条件
         */
        let result = await db.useraccount.findAndCountAll({limit: 10, order: ['id'], where: {id: 1}, offset: 2});
        ctx.body = {
            status: true,
            code: '00001',
            data: result
        }
    }
    /**
     * 获取单个数据详情
     * @param {Object} ctx 
     */
    static async fetch (ctx) {
        let { db, params: { id } } = ctx;
        if (!id) {
            return ctx.body = {
                code: '00002',
                data: null,
                message: '参数错误'
            }
        }
        let result = await db.useraccount.findOne({ where: {id}});
        ctx.body = {
            status: true,
            code: '00001',
            data: result
        }
    }
    /**
     * 修改数据
     */
    static async put (ctx) {
        const { db, params: { id }, request: { body } } = ctx;
        if (!id) {
            return ctx.body = {
                code: '00003',
                data: null,
                message: '参数错误'
            }
        }
        let result = await db.useraccount.update(body, {where: {id}});
        if (result) {
            return ctx.body = {
                code: '00001',
                data: result,
                message: '修改成功'
            }
        }
        ctx.body = {
            code: '00002',
            message: '未知错误',
            data: null
        }
    }
    /**
     * 删除数据
     */
    static async delete (ctx) {
        const {query, db} = ctx;
        let params = ctx.params;
        let id = Number(params.id);
        if (!id) {
            return ctx.body = {
                code: '00004',
                message: '参数错误',
                data: null
            }
        }
        let result = await db.useraccount.destroy({ where: {id}});
        if (result) {
            return ctx.body = {
                code: '00001',
                message: '删除成功',
                data: result
            }
        }
        ctx.body = {
            code: '00002',
            message: '未知错误',
            data: result
        }
        
    }
}